package com.tangsm.spring.boot.springdoc.controller;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.tangsm.spring.boot.springdoc.api.AdminSimulationApi;
import com.tangsm.spring.boot.springdoc.domain.base.Result;
import com.tangsm.spring.boot.springdoc.domain.dto.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;

/**
 * 模拟管理员操作接口示例（非数据库操作）
 *
 * @author tangsm
 * @date 2021-07-27 10:50:16
 */
@RestController
@RequestMapping("/admin-simulation")
public class AdminSimulationController implements AdminSimulationApi {

    private static final Logger log = LoggerFactory.getLogger(AdminSimulationController.class);

    @PostMapping("/add/{name}")
    @Override
    public Result<User> addUser(@PathVariable String name) {
        return Result.success(new User(1L, name, LocalDateTimeUtil.parseDate("2003-08-28"), 18, new BigDecimal("1000")));
    }

    @GetMapping("/del/{name}")
    @Override
    public Result<User> delUser(@PathVariable String name) {
        log.info("管理员删除name={}的用户", name);
        return Result.success(new User(2L, name, LocalDateTimeUtil.parseDate("2005-01-28"), 16, new BigDecimal("1000")));
    }

    @PostMapping("/update")
    @Override
    public Result<User> updateUser(@RequestBody User user) {
        user.setId(10L);
        user.setAge(100);
        log.info("管理员更新{}用户的年龄为{}", user, 100);
        return Result.success(user);
    }
}
